package com.streamer.app.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.bean.CouponBean

class CouponRecordAdapter(
    private val records: MutableList<CouponBean>
) : RecyclerView.Adapter<CouponRecordAdapter.RecordVH>() {

    class RecordVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvCouponName: TextView = itemView.findViewById(R.id.tv_coupon_name)
        val tvCouponType: TextView = itemView.findViewById(R.id.tv_coupon_type)
        val tvGrantNum: TextView = itemView.findViewById(R.id.tv_grant_num)
        val tvRemainingNum: TextView = itemView.findViewById(R.id.tv_remaining_num)
        val tvGrantTime: TextView = itemView.findViewById(R.id.tv_grant_time)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordVH {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_coupon_record, parent, false)
        return RecordVH(view)
    }

    override fun onBindViewHolder(holder: RecordVH, position: Int) {
        val record = records[position]

        holder.tvCouponName.text = record.couponName
        if (record.couponType == "1") {
            holder.tvCouponType.text = "满减卷"
        } else if (record.couponType == "2") {
            holder.tvCouponType.text = "折扣卷"
        }
        holder.tvGrantTime.text = record.createTime
        holder.tvGrantNum.text = record.grantNum.toString()
        holder.tvRemainingNum.text = (record.grantNum-record.receiveNum).toString()


    }

    override fun getItemCount(): Int = records.size

    fun updateRecords(newRecords: List<CouponBean>) {
        records.clear()
        records.addAll(newRecords)
        notifyDataSetChanged()
        Log.d("CouponRecordAdapter", "更新记录数据: ${records.size} 条")
    }

    fun addRecords(newRecords: List<CouponBean>) {
        val startPosition = records.size
        records.addAll(newRecords)
        notifyItemRangeInserted(startPosition, newRecords.size)
        Log.d("CouponRecordAdapter", "添加记录: ${newRecords.size} 条")
    }

    fun clearRecords() {
        records.clear()
        notifyDataSetChanged()
        Log.d("CouponRecordAdapter", "清空记录")
    }
} 